<script>
  import {
    getstructure,
    getSearhstructure
  } from '@/api/comment.js'
  import WechatApi from '@/libs/wechat.js';
  import {
    HTTP_REQUEST_URL
  } from './config/app';
  import {
    mapState,
    mapGetters,
    mapMutations,
    mapActions
  } from 'vuex'
  export default {
    globalData: {
      spid: 0,
      code: 0,
      appWidth: 320,
      appHeight: 500,
      animalList: []
    },
    onLaunch: function(option) {
      // console.log(option)
      let that = this;
      // #ifdef MP
      if (HTTP_REQUEST_URL == '') {
        console.error(
          "请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
        );
        return false;
      }
      if (option.query.hasOwnProperty('scene')) {
        // console.log(option.scene)
        switch (option.scene) {
          //扫描小程序码
          case 1047:
            //  let val = that.$util.getUrlParams(decodeURIComponent(option.query.scene));
            that.globalData.spid = option.query.scene;
            break;
            //长按图片识别小程序码
          case 1048:
            that.globalData.spid = option.query.scene;
            break;
            //手机相册选取小程序码
          case 1049:
            that.globalData.spid = option.query.scene;
            break;
            //直接进入小程序
          case 1001:
            that.globalData.spid = option.query.scene;
            break;
        }
      }
      // #endif
      // #ifdef MP-WEIXIN

      if (wx.canIUse('getUpdateManager')) {
        const updateManager = wx.getUpdateManager();
        updateManager.onCheckForUpdate(function(res) {
          // 请求完新版本信息的回调
          if (res.hasUpdate) {
            updateManager.onUpdateReady(function() {
              that.tui.modal('更新提示', '新版本已经上线啦~，为了获得更好的体验，建议立即更新', false, res => {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManager.applyUpdate();
              });
            });
            updateManager.onUpdateFailed(function() {
              // 新的版本下载失败
              that.tui.modal('更新失败', '新版本更新失败，为了获得更好的体验，请您删除当前小程序，重新搜索打开', false,
                res => {});
            });
          }
        });
      }
      // #endif

      // #ifdef MP
      // 小程序静默授权
      if (!this.$store.getters['m_user/isLogin']) {
        let spread = that.globalData.spid ? that.globalData.spid : 0;
        WechatApi.getCode()
          .then(code => {
            let data = {
              code: code,
              spreadId: spread
            };
            that.$store.dispatch('m_user/loginBySilenceAuth', data);
          });
      }
      // #endif
      getstructure().then(res => {
        that.globalData.animalList = res.data
        that.globalData.animalList.forEach(item => {
          item.categoryType = item.categoryType.split(',')
        })
        // console.log(that.globalData.animalList)
      })
    },
    onShow: function() {
      try {
        const res = uni.getSystemInfoSync();
        this.globalData.appWidth = typeof res.screenWidth === 'number' ? res.screenWidth : 320;
        this.globalData.appHeight = typeof res.screenHeight === 'number' ? res.screenHeight : 500;
        // this.globalData.benchmarkLevel = typeof res.benchmarkLevel === 'number' ? res.benchmarkLevel : -1;
        // wx.reportAnalytics('get_device_info', {
        //   device_info: JSON.stringify(res)
        // });
      } catch (e) {
        // console.log(e);
      }
    },
    onHide: function() {
      //console.log('App Hide')
    },
    onError: function(err) {
      //全局错误监听
      // #ifdef APP-PLUS
      plus.runtime.getProperty(plus.runtime.appid, widgetInfo => {
        const res = uni.getSystemInfoSync();
        let errMsg =
          `手机品牌：${res.brand}；手机型号：${res.model}；操作系统版本：${res.system}；客户端平台：${res.platform}；错误描述：${err}`;
        console.log('发生错误：' + errMsg);
      });
      // #endif
    }
  };
</script>

<style>
  @import './style/app.css';
</style>
